<template>
  <div>
    <bread second="商品管理" third="商品列表"></bread>
    <el-card class="box-card" style="margin-top:15px">
      <div style="margin-top:15px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input
              placeholder="请输入内容"
              class="input-with-select"
              v-model="goodObj.query"
              clearable
              @clear="getGoodList"
            >
              <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
            </el-input>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple-light"></div>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" @click="addGoods">添加商品</el-button>
          </el-col>
        </el-row>
      </div>
      <el-table :data="tableData" border style="width: 100%;margin-top:10px">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px"></el-table-column>
        <el-table-column label="创建时间" width="200px">
          <template slot-scope="scoped">{{ scoped.row.add_time | format }}</template>
        </el-table-column>
        <el-table-column prop="telephone" label="操作" width="130px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini">
              <i class="el-icon-edit"></i>
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">
              <i class="el-icon-delete"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top:10px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="goodObj.pagenum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="goodObj.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>
<script>
/* eslint-disable */
import { getGoodList_api, delGood_api } from '@/api/index.js'
export default {
  data() {
    return {
      tableData: [],
      goodObj: {
        query: '',
        pagenum: 2,
        pagesize: 10
      },
      total: null
    }
  },
  created() {
    this.getGoodList()
  },
  methods: {
    //获取商品列表
    async getGoodList() {
      const { data: res } = await getGoodList_api(this.goodObj)
      this.tableData = res.data.goods
      this.total = res.data.total
    },
    //添加商品
    addGoods() {
      this.$router.push('/goods/add').catch(err => { err })
    },
    //删除商品
    async handleDelete(row) {
      const res2 = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (res2 == 'confirm') {
        const { data: res } = await delGood_api(row.goods_id)
        console.log(row.goods_id, row, res, 123)
        this.getGoodList()
      }
    },
    //分页
    handleSizeChange(val) {
      this.goodObj.pagesize = val
      this.getGoodList()
      // console.log(`每页 ${val} 条`, val, '00000')
    },
    handleCurrentChange(val) {
      this.goodObj.pagenum = val
      this.getGoodList()
      // console.log(`当前页: ${val}`, val)
    }
  }
}
</script>
<style lang="less" scoped></style>
